package com.have.blog.admin.service.impl;

import com.have.blog.admin.domain.dto.UserDto;
import com.have.blog.admin.service.AdminUserService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.UserMapper;
import com.have.blog.model.models.User;
import com.have.blog.model.models.UserExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;
    
    @Override
    public Long getUserCount(int status) {
        UserExample example = new UserExample();
        example.createCriteria()
                .andStatusEqualTo(status);
        return userMapper.countByExample(example);
    }

    @Override
    public List<UserDto> getUserByIds(List<Integer> userUids) {
        if (CollectionUtils.isEmpty(userUids)) {
            return new ArrayList<>();
        }
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(userUids);
        List<User> users = userMapper.selectByExample(userExample);
        List<UserDto> userDtos = MyBeanUtils.copyListProperties(users, UserDto::new);
        List<Integer> uids = new ArrayList<>();
        for (UserDto userDto : userDtos) {
            if (userDto.getHeadUid() != null) {
                uids.add(userDto.getHeadUid());
            }
        }
        if (!CollectionUtils.isEmpty(uids)) {
            Wrapper<Map<Integer, String>> wrapper = pictureFeignApi.getPicturesUrl(uids);
            if (wrapper.isSuccess()) {
                Map<Integer, String> data = wrapper.getData();
                for (UserDto userDto : userDtos) {
                    userDto.setPhotoUrl(data.get(userDto.getHeadUid()));
                }
            }
        }
        return userDtos;
    }
}
